//
//  ChatTimeTipTableViewCell.m
//  TIMChat
//
//  Created by AlexiChen on 16/3/16.
//  Copyright © 2016年 AlexiChen. All rights reserved.
//

#import "ChatTimeTipTableViewCell.h"

@implementation ChatTimeTipTableViewCell

- (BOOL)canShowMenu
{
    return NO;
}

- (instancetype)initWithC2CReuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier])
    {
        self.contentView.backgroundColor = kClearColor;
        self.backgroundColor = kClearColor;
        self.textLabel.textAlignment = NSTextAlignmentCenter;
        self.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
        self.textLabel.numberOfLines = 0;
        
    }
    return self;
}
- (instancetype)initWithGroupReuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super init])
    {
        self.contentView.backgroundColor = kClearColor;
        self.backgroundColor = kClearColor;
        self.textLabel.textAlignment = NSTextAlignmentCenter;
        self.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
        self.textLabel.numberOfLines = 0;
    }
    return self;
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    // do nothing
}

- (void)relayoutFrameOfSubViews
{
    // do nothing
}


- (void)addC2CCellViews
{
    // do nothing
    
}

- (void)addGroupCellViews
{
    // do nothing
}

- (void)relayoutC2CCellViews
{
    // do nothing
}
- (void)relayoutGroupCellViews
{
//    [self.textLabel sizeToFit];
}

- (void)configKVO
{
    // do nothing
}

- (void)configWith:(IMAMsg *)msg
{
    _msg = msg;
    TIMCustomElem *elem = (TIMCustomElem *)[msg.msg getElem:0];
    self.textLabel.textAlignment = NSTextAlignmentCenter;
    self.textLabel.font = [_msg tipFont];
    self.textLabel.textColor = kLightGrayColor;
    self.textLabel.text = [elem timeTip];
    self.textLabel.backgroundColor = MHSpliteLineColor;
    self.textLabel.cornerRadius = 11.0f;
    self.textLabel.masksToBounds = YES;
//    self.textLabel.width = 50;

}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    self.textLabel.yf_contentInsets = UIEdgeInsetsMake(5 , 10, 5 , 10);
    [self.textLabel sizeToFit];
    self.textLabel.centerX = self.centerX;
    self.textLabel.y = (self.height - self.textLabel.height) / 2 ;
//     if([_msg type] == EIMAMSG_TimeTip)
//     {
//         self.textLabel.yf_contentInsets = UIEdgeInsetsMake(5 , 10, 5 , 10);
//         [self.textLabel sizeToFit];
//         self.textLabel.centerX = self.centerX;
//         self.textLabel.y = 7.5;
//     }else
//     {
//         self.textLabel.y = 0;
//     }

}

- (void)configContent
{
    
}

- (void)configElemContent
{
    
}
- (void)configSendingTips
{
    // do nothing
}



- (void)showMenu
{
    // do nothing
}

- (NSArray *)showMenuItems
{
    // do nothing
    return nil;
    
}

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    return NO;
}


@end


@implementation ChatGroupTipTableViewCell

- (void)configWith:(IMAMsg *)msg
{
    _msg = msg;
    TIMGroupTipsElem *elem = (TIMGroupTipsElem *)[msg.msg getElem:0];
//    IMAUser *senderUser = [msg getSender];
    self.textLabel.textAlignment = NSTextAlignmentCenter;
    self.textLabel.font = [_msg tipFont];
    self.textLabel.textColor = kLightGrayColor;
//    self.textLabel.text = [elem tipText];
    self.textLabel.backgroundColor = MHWhiteTextColor;
    self.textLabel.cornerRadius = 11.0f;
    self.textLabel.masksToBounds = YES;
    
    
    
    CustomElemCmd *elemCmd = [[IMAPlatform sharedInstance].conversationMgr  isOnlineMsg:elem];
    MHIMMessageType type  =  [[elemCmd.customInfo objectForKey:@"msgType"] intValue];
    if (type == MHIMMessageTypeRedPacketMessaege) {
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
        paragraphStyle.alignment = NSTextAlignmentCenter;
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@" %@",[elemCmd.customInfo objectForKey:@"commentTxt"]] ?:@"" attributes:@{NSParagraphStyleAttributeName:paragraphStyle                                                             }];
        NSTextAttachment *attchment = [[NSTextAttachment alloc]init];
        attchment.image = [UIImage imageNamed:@"ic_video_package_qn_small"];//设置图片
        attchment.bounds = CGRectMake(0, -5, 15, 18);//设置frame
         NSAttributedString *attachString = [NSAttributedString attributedStringWithAttachment:(NSTextAttachment *)(attchment)];
        [attributedString insertAttributedString:attachString atIndex:0];
        self.textLabel.attributedText = attributedString;
    }else if (type == MHIMMessageTypeTotalRedPacketMessage) //汇总消息
    {
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
        paragraphStyle.alignment = NSTextAlignmentCenter;
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@" %@",[elemCmd.customInfo objectForKey:@"commentTxt"]] ?:@"" attributes:@{NSParagraphStyleAttributeName:paragraphStyle }];
        if ([attributedString.string containsString:@"进入了圈子"]) {
            [attributedString addAttribute:NSForegroundColorAttributeName value:MHMomentScreenNameTextColor2 range:NSMakeRange(0, attributedString.string.length - 5)];
        }
        
        self.textLabel.attributedText = attributedString;
    }else if (type == MHIMMessageTypeJoinCircleName) //进入圈子,兼容聊天室
    {
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
        paragraphStyle.alignment = NSTextAlignmentCenter;
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@" %@",[elemCmd.customInfo objectForKey:@"joinUserName"]] ?:@"" attributes:@{NSParagraphStyleAttributeName:paragraphStyle }];
        [attributedString addAttribute:NSForegroundColorAttributeName value:MHMomentScreenNameTextColor2 range:NSMakeRange(0, attributedString.string.length)];
        [attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:@"进入了圈子"]];
        self.textLabel.attributedText = attributedString;
    }
    else
    {
        NSString *userNames = [elem getNickNamesWith:elem.userList];
         NSString * groupTip =  elem.groupTip;
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:[elem tipText]];
        if ([attributedString.string containsString:userNames]) {
            [attributedString addAttribute:NSForegroundColorAttributeName value:MHMomentScreenNameTextColor2 range:NSMakeRange(0, userNames.length)];
        }
        self.textLabel.attributedText = attributedString;
    }
    
   
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    self.textLabel.yf_contentInsets = UIEdgeInsetsMake(5 , 10, 5 , 10);
//    if([_msg type] == EIMAMSG_GroupTips)
//    {
//        self.textLabel.yf_contentInsets = UIEdgeInsetsMake(5 , 10, 5 , 10);
//        [self.textLabel sizeToFit];
//        self.textLabel.centerX = self.centerX;
//        self.textLabel.y = 7.5;
//    }else
//    {
//        self.textLabel.y = 0;
//    }
    
}

@end

@implementation ChatSaftyTipTableViewCell

- (void)configWith:(IMAMsg *)msg
{
    _msg = msg;
    TIMElem* elem = [msg.msg getElem:0];
    if ([elem isKindOfClass:[TIMCustomElem class]])
    {
        CustomElemCmd *elemCmd = [CustomElemCmd parseCustom:(TIMCustomElem *)elem];
        if (elemCmd)
        {
            self.textLabel.textAlignment = NSTextAlignmentCenter;
            self.textLabel.font = [_msg tipFont];
            self.textLabel.textColor = kLightGrayColor;
            self.textLabel.text = elemCmd.actionParam;
        }
    }
}

@end

@implementation RevokedTipTableViewCell

- (void)configWith:(IMAMsg *)msg
{
    _msg = msg;
    self.textLabel.textAlignment = NSTextAlignmentCenter;
    self.textLabel.font = [_msg tipFont];
    self.textLabel.textColor = kLightGrayColor;
    NSString *selfId = [IMAPlatform sharedInstance].host.loginParm.identifier;
    TIMCustomElem *elem = (TIMCustomElem *)[_msg.msg getElem:0];
    NSDictionary *info = [NSJSONSerialization JSONObjectWithData:elem.data options:NSJSONReadingMutableLeaves error:nil];
    NSString *msgSender = [info objectForKey:@"sender"];
    if ([selfId isEqualToString:msgSender]) {
        self.textLabel.text = [NSString stringWithFormat:@"你撤回了一条消息"];
    }
    else
    {
        self.textLabel.text = [NSString stringWithFormat:@"\"%@\" 撤回了一条消息",msgSender];
    }
}

@end
